package com.wavehi.homework.sixth;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;

/**
 * @Author WaveHi
 * @Date 25/8/2021 下午 8:53
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description Sixth homework : Mutual conversion of objects and collections.
 */
public class IntrospectionDemo1 {

    public <T> T collectionToObject(HashMap<String,Object> hm,Class<T> clz) {
        BeanInfo beanInfo = null;
        T t = null;

        try {
            //获取类的内省信息
            beanInfo = Introspector.getBeanInfo(clz);

            //获取对象
            t = clz.newInstance();
        } catch (IntrospectionException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        //获取属性
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();

        //给对象的属性赋值
        for(PropertyDescriptor propertyDescriptor : propertyDescriptors) {

            //获取属性的简单名称
            String name = propertyDescriptor.getName();
            //判断集合中是否有哦相应属性的值
            if(hm.containsKey(name)) {

                //获取设置器
                Method writeMethod = propertyDescriptor.getWriteMethod();

                try {
                    //设置对象相应的属性
                    writeMethod.invoke(t,hm.get(name));
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }

            }

        }

        //返回对象
        return t;

    }

    public <T> HashMap<String,Object> ObjectToCollection(T t) {

        //获取集合
        HashMap<String, Object> hm = new HashMap<>();

        //获取Class对象
        Class<?> aClass = t.getClass();
        BeanInfo beanInfo = null;

        try {
            //获取内省信息
            beanInfo = Introspector.getBeanInfo(aClass);
        } catch (IntrospectionException e) {
            e.printStackTrace();
        }

        //获取属性
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();

        //提取数据
        for(PropertyDescriptor propertyDescriptor : propertyDescriptors) {

            //获取取值器
            Method readMethod = propertyDescriptor.getReadMethod();

            try {
                //取值
                hm.put(propertyDescriptor.getName(),readMethod.invoke(t));
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }

        }

        //返回结果
        return hm;

    }

}

//小结：BeanInfo getBeanInfo(Class<?> beanClass) 获取指定Class对象对应类的内省信息
//     PropertyDescriptor[] getPropertyDescriptors() 获取所有属性组成的数组
//     Method getWriteMethod() 获取属性的设置器
//     Method getReadMethod() 获取属性的取值器